package com.jy.adscm.operation.purchase.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.purchase.bean.QueryByPageParameter;
import com.jy.adscm.operation.purchase.bean.Supplier;
import com.jy.adscm.operation.purchase.mapper.mysql.SupplierMapper;
import com.jy.adscm.operation.purchase.service.SupplierService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.security.SecureRandom;
import java.util.List;

/**
 * <p>
 * 供应商基础信息表 服务实现类
 * </p>
 *
 * @author lzf
 * @since 2019-05-05
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {
    @Autowired
    SupplierMapper supplierMapper;
    @Override
    public Page<Supplier> queryByCondition(QueryByPageParameter queryByPageParameter) throws AdscmSystemException{
        Page<Supplier> page=new Page<>(queryByPageParameter.getPage(),queryByPageParameter.getRows());
        Wrapper<Supplier> supplierWrapper=new EntityWrapper<>();
//        过滤
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryByPageParameter.getKey(),null)){
            supplierWrapper.like("name",queryByPageParameter.getKey()).or().
                    like("letter",queryByPageParameter.getKey().toUpperCase()).or()
                    .like("code",queryByPageParameter.getKey()).or()
                    .like("contacts",queryByPageParameter.getKey());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryByPageParameter.getSortBy(),null)){
            supplierWrapper.orderBy(queryByPageParameter.getSortBy(),queryByPageParameter.getDesc());
        }
        Page<Supplier> supplierPage = super.selectPage(page, supplierWrapper);
        supplierPage.setTotal(super.selectCount(supplierWrapper));

        return supplierPage;

    }

    @Override
    public List<Supplier> queryByName() {
        Wrapper<Supplier> wrapper=new EntityWrapper<>();
        wrapper.setSqlSelect("id","name");
        List<Supplier> suppliers = supplierMapper.selectList(wrapper);
        return suppliers;
    }
}
